<?php

namespace App\Http\Controllers\ApiHospital;

use App\Http\Controllers\Controller;
use App\Models\AppointQueue;
use App\Models\AppointQueueRoom;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class QueueController extends Controller
{
    public function list(){
        $user = Auth::guard('api-hospital')->user();
        $queues = AppointQueue::query()
            ->where('hospital_id',$user->hospital_id)
            ->where('status',1)
            ->where('appoint_date',date('Y-m-d'))
            ->orderBy('sort','asc')
            ->get();
        foreach ($queues as $k=>$v){
            $interval = HospitalAppointInterval::query()->where('id',$v->interval_id)->first();
            $v->interval = $interval->timeText();
            $v->room = "";
        }
        return response()->json(['code'=>200,'msg'=>'成功','data'=>$queues]);
    }
    public function room()
    {
        $user = Auth::guard('api-hospital')->user();
        $room = AppointQueueRoom::query()
            ->where('hospital_admin_user_id',$user->id)
            ->first();
        $type = HospitalAppointType::query()->get();
        $row['roomName'] = $room->name;
        $row['roomType'] = $room->type;

        return response()->json(['code'=>200,'msg'=>'成功','data'=>$row,'type'=>$type]);
    }
    public function roomSetting(Request $request)
    {
        $name = $request->input('roomName');
        $type = $request->input('roomTypeId');

        $user = Auth::guard('api-hospital')->user();

        $room = AppointQueueRoom::query()
            ->where('hospital_admin_user_id',$user->id)
            ->first();
        if(!$room) {
            $room = new AppointQueueRoom();
            $room->name = $name;
            $room->type = $type;
            $room->hospital_admin_user_id = $user->id;
            $room->save();
        }
        return response()->json(['code'=>200,'msg'=>'成功']);
    }
}
